package com.tools.service.impl;

import com.tools.commons.utils.BeanUtils;
import com.tools.model.entity.UserEntity;
import com.tools.model.entity.UserMsgRelEntity;
import com.tools.model.params.UserMsgParams;
import com.tools.repository.UserMsgRepository;
import com.tools.repository.UserRepository;
import com.tools.security.context.SecurityContextHolder;
import com.tools.service.UserMsgsService;
import com.tools.utils.SendSms;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserMsgSeriveImpl implements UserMsgsService {
    private final UserMsgRepository userMsgRepository;
    private final UserRepository userRepository;

    @Override
    public void save(UserMsgRelEntity userMsgRelEntity) {
        userMsgRepository.save(userMsgRelEntity);
        String userId = userMsgRelEntity.getTo();
        UserEntity byId = userRepository.findByUserId(userId);
        SendSms.sendNoticeTip(byId.getPhone());
    }

    @Override
    public List<UserMsgRelEntity> findMsgListByUserId(String userid) {

        List<UserMsgRelEntity> list = userMsgRepository.findByTo(userid);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus(1);
        }
        userMsgRepository.saveAll(list);
        return userMsgRepository.findByTo(userid);
    }


    @Override
    public int noReadNum(String userId) {
        userMsgRepository.countByToAndStatus(userId, 0);
        return userMsgRepository.countByToAndStatus(userId, 0);
    }


}
